<template>
  <div>
    <!-- 编辑弹窗 -->
    <cvue-dialog
      :dialogVisible="dialogVisible"
      :title="title"
      :dialogLoading="dialogLoading"
      dialogWidth="500px"
      @handleOpen="getDetail"
      @closeDialog="closeDialog"
      @confirmDialog="confirmDialog"
      @handleClose="handleClose">
      <div class="dialog-body" slot="dialogBody">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="字典键" prop="label">
                <el-input v-model.trim="ruleForm.label" :disabled="disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="字典值" prop="value">
                <el-input v-model.trim="ruleForm.value" :disabled="disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="字典名称" prop="name">
                <el-input v-model.trim="ruleForm.name" :disabled="disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </cvue-dialog>
  </div>
</template>
<script>
import {mapGetters} from 'vuex'
import cvueDialog from '@/components/cvue-dialog'
import { validPhone, validIp, validIdCN } from '@/util/validator.js'
export default {
  name: 'userEdit',
  components: {
    cvueDialog
  },
  data () {
    return {
      ruleForm: {
        label: '',
        value: '',
        name: ''
      },
      rules: {
        label: [
          { required: true, message: '请输入字典键', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '请输入字典值', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ]
      },
      disabled: false,
      dialogLoading: false
    }
  },
  mounted () {
    this.getEditDetail()
    this.getRoles()
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    title () {
      var title = ''
      if (this.$parent.actionType == 'add') {
        title = '新增'
      } else {
        title = '编辑'
      }
      return title
    },
    ...mapGetters(['token', 'userInfo', 'tenantId'])
  },
  methods: {
    // 触发事件
    handleClose () {
      this.$emit('handleClose')
    },
    closeDialog () {
      this.$emit('closeDialog')
    },
    confirmDialog () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          var params = {
            id: this.$parent.actionType == 'add' ? '' : this.$parent.rowData.id,
            label: this.ruleForm.label,
            value: this.ruleForm.value,
            name: this.ruleForm.name
          }
          // console.log(params)
          this.dialogLoading = true
          var url = this.$parent.actionType == 'add' ? 'manage/DictPost' : 'manage/DictPut'
          this.$store.dispatch(url, params).then(res => {
            // console.log(res)
            this.$message({
              message: res.msg,
              duration: 2000,
              type: 'success'
            })
            this.$emit('confirmDialog')
            this.dialogLoading = false
          }).catch(err => {
            this.$message({ message: err.msg, duration: 2000, type: 'error' })
            this.dialogLoading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getDetail () {
    },
    getEditDetail () {
      // console.log(this.$parent.rowData)
      if (this.$parent.actionType == 'edit') {
        this.$set(this.ruleForm, 'label', this.$parent.rowData.label)
        this.$set(this.ruleForm, 'value', this.$parent.rowData.value)
        this.$set(this.ruleForm, 'name', this.$parent.rowData.name)
      }
    }
  }
}
</script>
<style scoped>
</style>
